package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;

public class ListPage extends Utils {
    // 指定页面的 URL
    private static String url = "http://127.0.0.1:8080/list.html";
    // 设置显式等待
    private WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));

    public ListPage() {
        super(url);
    }

    // 测试页面是否正确加载
    public void listPageRight() {
        driver.findElement(By.cssSelector("#exampleInputName2"));
        // 获取页面标题
        String title = driver.getTitle();
        // 验证当前页面标题信息
        assert title.equals("在线音乐列表");
    }

    // 检查音乐的查询功能
    public void selectMusic() {
        WebElement input = driver.findElement(By.cssSelector("#exampleInputName2"));
        input.sendKeys("李宇春 - 咏春");
        WebElement select = driver.findElement(By.cssSelector("#submit1"));
        select.click();
        List<WebElement> result = driver.findElements(By.cssSelector("#info > tr"));
        assert result.size() == 1;

        input.clear();
        input.sendKeys("南征北战");
        select.click();
        result = driver.findElements(By.cssSelector("#info > tr"));
        assert result.size() >= 1;

        input.clear();
        select.click();
        result = driver.findElements(By.cssSelector("#info > tr"));
        assert result.size() >= 1;

        input.clear();
        input.sendKeys("没有这个音乐");
        select.click();
        result = driver.findElements(By.cssSelector("#info > tr"));
        assert result.size() == 0;
        input.clear();
    }

    // 检查删除选中音乐的功能
    public void deleteSelMusic() {
        driver.findElement(By.cssSelector("#exampleInputName2")).sendKeys("");
        driver.findElement(By.cssSelector("#submit1")).click();
        List<WebElement> result = driver.findElements(By.cssSelector("#info > tr"));
        int i = result.size();
        if (i > 10) {
            driver.findElement(By.cssSelector("#\\31 " + (i % 10 - 1))).click();
        }else {
            driver.findElement(By.cssSelector("#\\3" + (i - 1))).click();
        }
        driver.findElement(By.cssSelector("#delete")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        assert alert.getText().equals("音乐全部删除成功! ");
        alert.accept();
        i--;

        driver.navigate().refresh();
        int count = 3;
        assert count <= i;
        while (count > 0 && i > 10) {
            driver.findElement(By.cssSelector("#\\31 " + (i % 10 - 1))).click();
            i--;
            count--;
        }
        while (count > 0) {
            driver.findElement(By.cssSelector("#\\3" + (i - 1))).click();
            i--;
            count--;
        }
        driver.findElement(By.cssSelector("#delete")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        assert alert.getText().equals("音乐全部删除成功! ");
        alert.accept();

        driver.findElement(By.cssSelector("#delete")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        assert alert.getText().equals("批量删除音乐失败!! ");
        alert.accept();
    }

    // 检查删除音乐的功能
    public void deleteMusic() {
        driver.findElement(By.cssSelector("#exampleInputName2")).sendKeys("");
        driver.findElement(By.cssSelector("#submit1")).click();
        int index = driver.findElements(By.cssSelector("#info > tr")).size();
        driver.findElement(By.cssSelector("#del" + (index - 1))).click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        assert alert.getText().equals("删除音乐成功! ");
        alert.accept();
    }

    // 检查喜欢音乐的功能
    public void loveMusic() {
        driver.findElement(By.cssSelector("#exampleInputName2")).sendKeys("");
        driver.findElement(By.cssSelector("#submit1")).click();
        int index = driver.findElements(By.cssSelector("#info > tr")).size();
        driver.findElement(By.cssSelector("#love" + (index - 1))).click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        assert alert.getText().equals("喜欢成功! ");
        alert.accept();
    }
}